package edu.bu.cs683.traveldroid;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import edu.bu.cs683.traveldroid.data.PointOfInterest;

/**
 * This class implements an ItemizedOverlay object that holds the list of Point
 * Of Interest Overlays that should be displayed over the Map.
 */
public class POIItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
	private List<PointOfInterest> pois = new ArrayList<PointOfInterest>();
	private Activity activity;

	/**
	 * Constructor
	 * 
	 * @param defaultMarker
	 * @param pointsOfInterest
	 * @param theActivity
	 */
	public POIItemizedOverlay(Drawable defaultMarker,
			List<PointOfInterest> pointsOfInterest, Activity theActivity) {
		super(boundCenterBottom(defaultMarker));

		pois = pointsOfInterest;
		activity = theActivity;
	}

	/**
	 * Adds a Overlay to the list.
	 * 
	 * @param overlay
	 */
	public void addOverlay(OverlayItem overlay) {
		overlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return overlays.get(i);
	}

	@Override
	public int size() {
		return overlays.size();
	}

	/**
	 * Handles the user tapping on a POI icon. This results in the detail for
	 * the selected POI being displayed in the POIDetailActivity
	 */
	@Override
	protected boolean onTap(int i) {

		// Set the current Point Of Interest
		TravelDroidApplication application = (TravelDroidApplication) activity
				.getApplication();
		application.setSelectedPOI(pois.get(i));

		// Start the POIDetail Activity
		Intent intent = new Intent(
				POIDetailActivity.INTENT_ACTION_VIEW_POI_DETAIL);
		activity.startActivity(intent);
		return (true);
	}

}
